package dcl.software.filesync.server.auth;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;

import dcl.software.filesync.server.BaseRepository;

/*@Repository
 @DependsOn("mongoClient")*/
public class UserRepositoryImpl extends BaseRepository implements CustomUserRepository {
	@Value("${mongo.collection.users}")
	private String usersCollection;

	@Override
	protected String getCollectionString() {
		return usersCollection;
	}

	@Override
	public void saveUser(User userName) {
		MongoCollection<Document> collectionSession = getCollectionSession();
		try {
			collectionSession.insertOne(new Document(userName.getPropertyMap()));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Document findUser(String userName) {
		MongoCollection<Document> collectionSession = getCollectionSession();
		FindIterable<Document> find = collectionSession.find(new Document("_id", userName));
		MongoCursor<Document> iterator = find.iterator();
		if (iterator.hasNext())
			return iterator.next();
		return null;
	}

	@Override
	public void deleteUser(Object id) {
		MongoCollection<Document> collectionSession = getCollectionSession();
		collectionSession.deleteOne(new Document("_id", id));
	}
}
